import numpy as np
import matplotlib.pyplot as plt

from view.endSrc.tGammaClustersEstimatorBy1D import tGammaClustersEstimatorBy1D
from view.endSrc.GammaClustersEstimatorBy1D import GammaClustersEstimatorBy1D
from view.endSrc.DBConfig import DBConfig
from view.endSrc.MySqlConn import MySqlConn


def gammaEst():
    x = np.loadtxt(open('mixgamma_shape_5_30_100_scale_1_1_1_pai_01_04_05.csv', 'rb'), delimiter=',')
    ra = range(1, 15)

    aiccList = []
    for M in ra:
        cfg = DBConfig()
        sqlConn = MySqlConn(cfg)

        # create a row of the table tGammaClustersEstimatorBy1D
        table = tGammaClustersEstimatorBy1D(sqlConn)
        table.createTable()
        table.setOptParams(maxRestarts=20, verb=False)
        rowId = table.createRow(observerId=9, nComponents=M)

        # call Estimator
        gce = GammaClustersEstimatorBy1D()
        gce.setConfig(sqlConn, rowId)

        gammaClusters, ll = gce.estimate(x)
        aiccList.append(ll)
        print('-------------------------------------------------------------------------------------------------')


    print(aiccList)
    plt.plot(aiccList)
    plt.show()


gammaEst()